package com.coffee.price;

import java.text.NumberFormat;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cauly.android.ad.AdInfo;
import com.cauly.android.ad.AdView;
import com.coffee.price.common.BasicActivity;
import com.coffee.price.common.Const;
import com.coffee.price.common.XMLParser;

public class DiscountActivity extends BasicActivity {
	
public RelativeLayout cauly;
	
	public ListView MyList = null;
	private List<Map<String,String>> list;
	private ProgressDialog progressDialog;

	public Thread dataThread = null;
	public Handler handler = new Handler();
	final private int PROGRESS_DIALOG = 0;
	
	public String brandCode;

	private int rows = 0;
	private LinearLayout disc_list;
	
	private ImageView brand_discount;
	
	private NumberFormat format = null;;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.discount_list);
        
        brandCode = getIntent().getStringExtra("brand_code");
        
        //===========타이틀 영역===========
        txt_title = (TextView)findViewById(R.id.txt_title);
        //txt_title.setText("할인 정보");
        
        btn_title_left = (ImageButton)findViewById(R.id.btn_title_left);
        btn_title_left.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_back));
        
        btn_title_left.setOnClickListener(this);
        //===========타이틀 영역===========
        
        //부모 레이아웃.
        cauly = (RelativeLayout)findViewById(R.id.cauly_layout);
        
        brand_discount = (ImageView)findViewById(R.id.brand_discount);
        brand_discount.setImageDrawable(getResources().getDrawable(Const.disc_img[Integer.parseInt(brandCode)]));
        
        //데이터 채우기
        AdInfo info = new AdInfo();
        info.initData("w8LQXZAq0F", "cpc", "all", "all", "off", "circle", "yes", 90);
        
        //광고 View
        AdView adView = new AdView(this);
        
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);	
        //부모 레이어 하단에 위치할때.
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        
        adView.setVisibility(View.VISIBLE);
		adView.setLayoutParams(params);
		//adView.setAdListener(this);
		
		//레이아웃 적용.
		cauly.addView(adView);
		
		disc_list = (LinearLayout) findViewById(R.id.disc_list);
		
		//숫자 변환
		format = NumberFormat.getInstance();
		format.setMaximumIntegerDigits(6); //최대수 지정
		
		showDialog(PROGRESS_DIALOG);
    	
    	dataThread = new Thread(null, xmlData);
    	dataThread.start();
    	
    }
    
    public void onClick(View v) {
    	
    	if(v == btn_title_left){
    		finish();
    	}
    }

    
    
    /**
     * <pre>
     * historyLoad
     * void
     * </pre>
     */
    public void BrandForDiscount(){
    	
    	//테스트 히스토리 데이터를 가져 온다.
    	rows = list.size();
    	disc_list.removeAllViews();
        for(int ii = 0 ; ii <  rows ; ii++){
        	disc_list.addView(getDiscRow(list.get(ii)));
        }
    }
    
    public View getDiscRow(Map<String,String> data){
    	
    	LayoutInflater inflater = getLayoutInflater();
		View v = inflater.inflate(R.layout.discount_row, null);
		
		TextView discount_name = (TextView)v.findViewById(R.id.discount_name);
		discount_name.setText(data.get("discount_name"));
		
    	return v;
    }
    
    private Runnable xmlData = new Runnable() {
    	public void run() {
    		try {
    			
    			XMLParser parser = new XMLParser(Const.discount_url+brandCode);
    		    list = parser.discount();
		    	handler.post(dataResults);
    		} catch (Exception e) {
    			Log.d("partData : ", e.toString());
    		}

    	}
    };
    
	private Runnable dataResults = new Runnable() {
    	public void run() {
    		try {
    			if (list != null) {
    				BrandForDiscount();
    			}
    			else{
    				showNotFoundData();
    			}
    			//2초 딜레이
    			Thread.sleep(2000);
    			
    	    	progressDialog.dismiss();
    	    	DiscountActivity.this.removeDialog(PROGRESS_DIALOG);
    	    	
    		} catch (Exception e) {
    			Log.e("xml-data", e.toString());
    		}

    	}
    };
    
    private void showNotFoundData() {
    	Toast toast = Toast.makeText(this, "할인 정보를 가져오지 못했습니다.", Toast.LENGTH_SHORT);
    	toast.show();
    }

	@Override
	protected Dialog onCreateDialog (int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			
			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("할인 정보 로딩중 입니다..");
			return progressDialog;
		default:
			
			return null;
		}
	}
	
}